package com.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bean.TestCaseFieldsForm;
import com.bean.TestCaseForm;
import com.dao.TestCaseDAO;
import com.entity.FieldType;
import com.entity.TestCase;
import com.entity.TestFun;
import com.google.gson.Gson;

@Controller
public class WelcomeController {

	@Autowired
	public TestCaseDAO testCaseDao;

	@RequestMapping(value = "/")
	public ModelAndView welcome() {
		ModelAndView mav = new ModelAndView("Welcome");

		return mav;
	}

	
	//Tree Loading
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/TreeLoading/{name}/{nodeType}")
	public void treeLoading(HttpServletRequest req,
			HttpServletResponse response, @PathVariable String name,
			@PathVariable String nodeType) {
		System.out.println("inside treeLoading");
		response.setContentType("application/json");
		System.out.println("parent" + name + "nodeType" + nodeType);
		if (nodeType.equalsIgnoreCase("root")) {
			System.out.println("selected one is root");
			List<TestFun> list = testCaseDao.listTestFunByparent(name);
			JSONObject j = new JSONObject();
			j.put("name", name);
			j.put("id", name);
			j.put("nodeType", "root");
			JSONArray j1 = new JSONArray();
			for (int i = 0; i < list.size(); i++) {
				TestFun tree = list.get(i);
				JSONObject j11 = new JSONObject();
				j11.put("name", tree.getTfName());
				j11.put("id", tree.getId());
				j11.put("nodeType", tree.getNodeType());
				j11.put("parent", tree.getParent());
				List<TestCase> list1 = testCaseDao.listTestCaseByParent(tree
						.getTfName());
				if (!list1.isEmpty()) {
					System.out.println("node " + tree.getTfName()
							+ " has childs");
					j11.put("children", true);
				}
				System.out.println("loop num" + i);
				j1.add(j11);
			}
			j.put("children", j1);
			try {
				j.writeJSONString(response.getWriter());
			} catch (Exception e) {
				e.printStackTrace();
			}
			System.out.println("end of root Tree");

		} else if (nodeType.equalsIgnoreCase("parent")) {
			System.out.println("selected one is parent");
			List<TestCase> list = testCaseDao.listTestCaseByParent(name);
			JSONObject j = new JSONObject();
			j.put("name", name);
			j.put("id", name);
			j.put("nodeType", "parent");
			JSONArray j1 = new JSONArray();
			for (int i = 0; i < list.size(); i++) {
				TestCase tree = list.get(i);
				JSONObject j11 = new JSONObject();
				j11.put("name", tree.getTestCaseName());
				j11.put("id", tree.getId());
				j11.put("nodeType", tree.getNodeType());
				j11.put("parent", tree.getParent());
				List<TestCase> list1 = testCaseDao.listTestCaseByParent(tree
						.getTestCaseName());
				if (!list1.isEmpty()) {
					System.out.println("node " + tree.getTestCaseName()
							+ " has childs");
					j11.put("children", true);
				}

				System.out.println("loop no " + i);
				j1.add(j11);
			}
			j.put("children", j1);
			try {
				j.writeJSONString(response.getWriter());
			} catch (Exception e) {
				e.printStackTrace();
			}
			System.out.println("end of parent Tree");
		}

	}


//	@RequestMapping(method = RequestMethod.POST, value = "/addTestCase", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
//	public @ResponseBody
//	String addTestCase(HttpServletResponse response,HttpSession session,
//			@RequestBody String testCase) {
//
//		System.out.println(">>>Called controller, customers ="
//				+ testCase.toString());
//		try {
//			Gson gson = new Gson();
//			TestCase ts = gson.fromJson(testCase, TestCase.class);
//			
//		
//			ts.setNodeType("children");
//			System.out
//					.println("ts" + ts.getTestcaseNum() + ts.getTestCaseName()
//							+ ts.getDescription() + ts.getType()+"parent"+ts.getParent());
//			testCaseDao.saveTestCase(ts);
//         
//         
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return testCase;
//	}

	
	@RequestMapping(method = RequestMethod.POST, value = "/addTestCase/{tf}")
	public @ResponseBody
	void addTestCase(HttpServletResponse response,HttpSession session,
			@ModelAttribute TestCaseForm ts,@PathVariable String tf) {
		System.out.println(">>>Start of addTestCase .........................................................");
System.out.println("names"+ts.getTestCaseName()+"no"+ts.getTestCaseNum()+"desc"+ts.getDescription()+"type"+ts.getType()+"parent"+tf);
List<String> tcNames = ts.getTestCaseName();
List<String> tcNum =ts.getTestCaseNum();
List<String> tcDesc = ts.getDescription();
List<String> tcType = ts.getType();
for(int i=0;i<tcNames.size();i++)
{
	System.out.println("loop"+i);
	TestCase tc = new TestCase();
	tc.setTestcaseNum(tcNum.get(i));
	tc.setTestCaseName(tcNames.get(i));
	tc.setDescription(tcDesc.get(i));
	tc.setType(tcType.get(i));
	tc.setNodeType("children");
	tc.setParent(tf);
	testCaseDao.saveTestCase(tc);
}

System.out.println(">>>end of addTestCase .........................................................");

	}

	
	
//public TestCase	prepareTc(TestCaseForm tc)
//{
//	TestCase tc1=new TestCase();
//	tc1.setTestcaseNum(tc.getTestCaseNum());
//	tc1.setTestCaseName(tc.getTestCaseName());
//	tc1.setDescription(tc.getDescription());
//	tc1.setNodeType("children");
//	tc1.setType(tc.getType());
//	tc1.setParent("Tf04");
//	return tc1;
//}
//	
	
	
//	@RequestMapping(method = RequestMethod.POST, value = "/addTestCaseFields/{tc}/{tf}", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
//	public @ResponseBody
//	String addTestCaseFields(HttpServletResponse response,HttpSession session,@PathVariable String tc,@PathVariable String tf,
//			@RequestBody String testCaseFields) {
//
//		System.out.println("TestCase Fields String"	+ testCaseFields.toString());
//		System.out.println("Tc and Tf ="
//				+ tc+","+tf);
//		try {
//			Gson gson = new Gson();
//			FieldType ft = gson.fromJson(testCaseFields, FieldType.class);
//			
//		
//			ft.setTestCaseName(tc);
//			ft.setTestFunName(tf);
//		
//			testCaseDao.saveTestCaseFields(ft);
//         
//         
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return testCaseFields;
//	}

	@RequestMapping(method = RequestMethod.POST, value = "/addTestCaseFields/{tc}/{tf}")
	public @ResponseBody
	void addTestCaseFields(HttpServletResponse response,HttpSession session,@PathVariable String tc,@PathVariable String tf,
			@ModelAttribute TestCaseFieldsForm tff) {

		System.out.println("...........................TestCase Fields String.......................................");

		List<String> tName = tff.getFieldName();
		List<String> tValue = tff.getFieldValue();
		List<String> tper = tff.getPermission();
		List<String> tXpath = tff.getXpath();
		List<String> tType = tff.getFieldtype();
		
		for(int i=0;i<tName.size();i++)
		{
			FieldType ft = new FieldType();
			ft.setFieldName(tName.get(i));
			ft.setFieldType(tType.get(i));
			ft.setFieldValue(tValue.get(i));
			ft.setPermision(tper.get(i));
			ft.setTestCaseName(tc);
			ft.setTestFunName(tf);
			ft.setXpath(tXpath.get(i));
			testCaseDao.saveTestCaseFields(ft);
		}
		
		
	}
//Hello i am update
	
	
}
